﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using ConexionCliente;
using ConexionCliente.Responses;

namespace Cliente
{
    class RequestManager
    {

        private Config conf = null;
        private List<ServResponse> servidores = new List<ServResponse>(); 
        public ClientForm ventana = null;
        private IServicioCliente remoto;

        public RequestManager(Config conf, ClientForm form)
        {
            this.conf = conf;
            this.ventana = form;

            String conn = "tcp://" + conf.Ip_manager + ":" + conf.Puerto_manager + "/Cliente";
            //TcpChannel chan = new TcpChannel();
            //ChannelServices.RegisterChannel(chan, false);

            remoto = (IServicioCliente)Activator.GetObject(typeof(IServicioCliente), conn);
        }

        public void ConsultarMemoriaManager()
        {
            try{
                ServResponse s = (ServResponse)(ventana.getListBox().SelectedItem);
                ventana.setRam(remoto.ObtenerRam(s.Id).RamTotal.ToString(), remoto.ObtenerRam(s.Id).RamLibre.ToString());
                
            }
            catch (Exception)
            {
                MessageBox.Show("No se pudo realizar conexion con el manager.");
            }
        }

        public void ConsultarCPUManager()
        {
            try
            {
                ServResponse s = (ServResponse)(ventana.getListBox().SelectedItem);
                ventana.setCpu(remoto.ObtenerCPU(s.Id).CpuUsage.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("No se pudo realizar conexion con el manager.");
            }
        }

        public void ConsultarDiscoManager()
        {
            try
            {
                ServResponse s = (ServResponse)(ventana.getListBox().SelectedItem);
                ventana.setDisco(remoto.ObtenerDisco(s.Id).CapacidadTotal.ToString(), remoto.ObtenerDisco(s.Id).CapacidadLibre.ToString());
                
            }
            catch (Exception)
            {
                MessageBox.Show("No se pudo realizar conexion con el manager.");
            }
        }

        public int ConsultarServidoresManager()
        {
            try
            {
                servidores = remoto.ObtenerServidores();
                ListBox lista = (ListBox)ObtenerControl("lstServers");
                lista.DataSource = servidores;
                return servidores.Count;

            }
            catch (Exception)
            {
                MessageBox.Show("No se pudo realizar conexion con el manager.");
                return 0;
            }
        }

        private Control ObtenerControl(string cmd)
        {
            Control[] controles = ventana.Controls.Find(cmd, true);

            return controles[0];
        }

    }
}
